package com.ruoyi.system.service.impl;

import java.util.List;
import java.util.Map;
import java.util.HashMap;
import java.util.ArrayList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.BigScreenMapper;
import com.ruoyi.system.domain.SpffVideos;
import com.ruoyi.system.service.IBigScreenService;

/**
 * 数据大屏Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-10-15
 */
@Service
public class BigScreenServiceImpl implements IBigScreenService 
{
    @Autowired
    private BigScreenMapper bigScreenMapper;

    /**
     * 获取地图数据（用户分布或视频分布）
     * 
     * @param type 数据类型：user-用户分布, video-视频分布
     * @return 地图数据列表
     */
    @Override
    public List<Map<String, Object>> getMapData(String type)
    {
        try {
            if ("user".equals(type)) {
                // 获取用户地理分布数据
                return bigScreenMapper.getUserDistribution();
            } else {
                // 获取视频地理分布数据
                return bigScreenMapper.getVideoDistribution();
            }
        } catch (Exception e) {
            // 返回空列表而不是抛出异常
            return new ArrayList<>();
        }
    }

    /**
     * 获取热度视频TOP排行榜
     * 
     * @param size 返回数量
     * @return 热度视频列表
     */
    @Override
    public List<SpffVideos> getHotVideos(Integer size)
    {
        try {
            if (size == null || size <= 0) {
                size = 10;
            }
            return bigScreenMapper.getHotVideos(size);
        } catch (Exception e) {
            return new ArrayList<>();
        }
    }

    /**
     * 获取实时统计数据
     * 
     * @return 统计数据
     */
    @Override
    public Map<String, Object> getRealTimeStats()
    {
        Map<String, Object> stats = new HashMap<>();
        
        try {
            // 总用户数
            Integer totalUsers = bigScreenMapper.getTotalUsers();
            stats.put("totalUsers", totalUsers != null ? totalUsers : 0);
            
            // 总视频数
            Integer totalVideos = bigScreenMapper.getTotalVideos();
            stats.put("totalVideos", totalVideos != null ? totalVideos : 0);
            
            // 总观看数
            Long totalViews = bigScreenMapper.getTotalViews();
            stats.put("totalViews", totalViews != null ? totalViews : 0);
            
            // 总点赞数
            Long totalLikes = bigScreenMapper.getTotalLikes();
            stats.put("totalLikes", totalLikes != null ? totalLikes : 0);
            
            // 总评论数
            Long totalComments = bigScreenMapper.getTotalComments();
            stats.put("totalComments", totalComments != null ? totalComments : 0);
            
        } catch (Exception e) {
            // 返回默认值
            stats.put("totalUsers", 0);
            stats.put("totalVideos", 0);
            stats.put("totalViews", 0);
            stats.put("totalLikes", 0);
            stats.put("totalComments", 0);
        }
        
        return stats;
    }
}

